package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

//@Controller
@Api(tags = "01.咨询管理模块")
@RestController
@RequestMapping("/v1/notice/")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    @ApiOperation("添加咨询")
    @ApiOperationSupport(order = 10)
//    @ResponseBody
    public JsonResult add(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = " + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        log.debug("notice = " + notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice = " + notice);
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @ApiOperation("咨询列表")
    @ApiOperationSupport(order = 20)
//    @ResponseBody
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery = " + noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO>noticeListVOS = noticeMapper.list(title,type);
        return JsonResult.ok(noticeListVOS);
    }
    @GetMapping("detail")
    @ApiOperation("咨询详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "咨询编号", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "咨询标题", required = true, dataType = "String")
    })
//    @ResponseBody
//    @ApiImplicitParam(name = "id", value = "咨询编号", required = true, dataType = "Long")
//    @ApiImplicitParam(name = "title", value = "咨询标题", required = true, dataType = "String")
    public JsonResult detail(Long id, String title){
        log.debug("id = " + id);
        NoticeDetailInfoVO detailInfoVO = noticeMapper.detail(id);
        return JsonResult.ok(detailInfoVO);
    }
    @PostMapping("del")
    @ApiOperation("删除咨询")
    @ApiOperationSupport(order = 40)
    @ApiImplicitParam(name = "id", value = "咨询编号", required = true, dataType = "Long")
//    @ResponseBody
    public JsonResult del(Long id){
        log.debug("id = " + id);
        int i = noticeMapper.del(id);
        if(i > 0) {
            return  JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FALSE);
        }
    }
    @PostMapping("update")
    @ApiOperation("更新咨询")
    @ApiOperationSupport(order = 50)
//    @ResponseBody
    public JsonResult update(@ApiIgnore NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.update(notice);
        if(i > 0) {
            return  JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FALSE);
        }
    }
}
